package openGLTest.code;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;

import android.graphics.Color;
import android.opengl.GLU;

public class GLThread extends Thread {

	private final GLView view;
	private boolean running = true;
	private final GLCube cube = new GLCube();
	private long rotationtime;

	private EGL10 egl10;
	private GL10 gl10;
	private EGLDisplay eglDisplay;
	private EGLSurface eglWindowSurface;
	private EGLContext eglContext;
	private EGLConfig eglConfig;

	public GLThread(GLView view) {

		this.view = view;
	
	}

	@Override
	public void run() {
		init();
		
		while (running) {
			
			draw();
			egl10.eglSwapBuffers(eglDisplay, eglWindowSurface);

		}
		shutdown();
	}

	private void init() {
		egl10 = (EGL10) EGLContext.getEGL();
		eglDisplay = egl10.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
		egl10.eglInitialize(eglDisplay, null);

		int[] conficspecification = { EGL10.EGL_RED_SIZE, 0,
				EGL10.EGL_GREEN_SIZE, 0, EGL10.EGL_BLUE_SIZE, 0,
				EGL10.EGL_DEPTH_SIZE, 0, EGL10.EGL_NONE };

		int[] num_config = new int[1];
		egl10.eglGetConfigs(eglDisplay, null, 0, num_config);

		EGLConfig[] eglConfigs = new EGLConfig[num_config[0]];

		egl10.eglChooseConfig(eglDisplay, conficspecification, eglConfigs,
				eglConfigs.length, num_config);

		eglConfig = eglConfigs[0];

		eglContext = egl10.eglCreateContext(eglDisplay, eglConfig,
				EGL10.EGL_NO_CONTEXT, null);

		gl10 = (GL10) (eglContext.getGL());

		eglWindowSurface = egl10.eglCreateWindowSurface(eglDisplay, eglConfig,
				view.getHolder(), null);

		egl10.eglMakeCurrent(eglDisplay, eglWindowSurface, eglWindowSurface,
				eglContext);

		gl10.glEnable(GL10.GL_DEPTH_TEST);

		gl10.glDepthFunc(GL10.GL_LEQUAL);

		gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		gl10.glEnableClientState(GL10.GL_NORMAL_ARRAY);

		gl10.glEnableClientState(GL10.GL_COLOR_ARRAY);

		rotationtime = System.currentTimeMillis();

		setPerspective();
		
		//setMaterial();
		
		//setTexture();
		
		//setLight();

	}

	private void setPerspective() {

		gl10.glViewport(0, 0, view.getWidth(), view.getHeight());
		gl10.glMatrixMode(GL10.GL_PROJECTION);
		gl10.glLoadIdentity();

		float ratio = (float)view.getWidth() / view.getHeight();
		GLU.gluPerspective(gl10, 50.0f, ratio, 1.0f, 100.0f);
	}

	private void draw() {

		gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		gl10.glMatrixMode(GL10.GL_MODELVIEW);

		gl10.glLoadIdentity();

		gl10.glTranslatef(0, 0, -6.0f);

		long elapsed = System.currentTimeMillis() - rotationtime;

		gl10.glRotatef(elapsed * (45.0f / 1000f), 0, 1, 0);

		gl10.glRotatef(elapsed * (45.0f / 1000f), 1, 0, 0);

		gl10.glRotatef(elapsed * (45.0f / 1000f), 0, 0, 1);

		cube.draw(gl10);

	}

	private void shutdown() {

		egl10.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE,
				EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);

		egl10.eglDestroySurface(eglDisplay, eglWindowSurface);

		egl10.eglDestroyContext(eglDisplay, eglContext);

		egl10.eglTerminate(eglDisplay);

	}
	
	private void setLight(){
		float lightAmbient[] = new float[] {0.3f,0.3f,0.3f,1.0f};
		float lightDiffuse[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		float lightSpecular[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		float lightPosition[] = new float[] {1.0f,1.0f,2.0f,1.0f};
		
		gl10.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
		gl10.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
		gl10.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecular, 0);
		gl10.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
		gl10.glEnable(GL10.GL_LIGHTING);
		gl10.glEnable(GL10.GL_LIGHT0);
	}
	
	private void setMaterial(){
		float materialAmbient[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		float materialDiffuse[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		float materialSpecular[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		float materialEmission[] = new float[] {1.0f,1.0f,1.0f,1.0f};
		
		gl10.glEnable(GL10.GL_COLOR_MATERIAL);
		gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, materialAmbient, 0);
		gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, materialDiffuse, 0);
		gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, materialSpecular, 0);
		gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, materialEmission, 0);
		
		gl10.glEnable(GL10.GL_FRONT_AND_BACK);
		
	}
	
	private void setTexture() {
		
		gl10.glEnable(GL10.GL_TEXTURE_COORD_ARRAY);
		gl10.glEnable(GL10.GL_TEXTURE_2D);
		cube.loadTexture(gl10,view.getContext(),R.drawable.texture);
	}

	public void stoptread() {

		running = false;
		try {
			join();
		} catch (Exception e) {
		}

	}
	
	
}
